#pragma once

#include "IManager.h"
#include "servermessagemanager.h"
#include <Windows.h>
#include <QVector>

class ServerManager : public IManager
{
    Q_OBJECT

public:
    ServerManager(const QString& nickname, QGraphicsScene * scene, QVector<int> &startPositions);
    ~ServerManager();

public slots:
    void slotKeyPresEvent(QKeyEvent *event);
    void slotKeyReleaseEvent(QKeyEvent *event);

    void slotCreateMap(int width, int height, const QString& map, int health, int motion_speed, int bullet_speed, int fire_rate);

    // Call this method to set nickname of a player
    // param tank_id (OUT) - assigned id if non-negative, if nickname is not allowed then id is negative
    virtual void slotSetNickname(int localDescriptor, QString nick);
    virtual void slotRequestPlaylist(int socketDescriptor);
    virtual void slotRequestMap();

    virtual void slotDisconnect(int player_id);

    void slotUpdate();
    void slotStart();
    void slotFire(int tank_id);
    void slotStopFire(int tank_id);
    void slotMove(int tank_id);
    void slotTurn(int tank_id, char direction);
    void slotStopMove(int tank_id);

signals:
    // This signal is for Game Creator
    void signalStart();


    // These signals are for both Game Creator and Message Manager
    void signalGameOver(int player_id);
    // param playerlist (IN):
    //     SC_PLAYERLIST<space><id 1><space><nickname 1><space>...<id n><space><nickname n>
    void signalPlaylist(int localDescriptor, QString playerlist);
    void signalPlaylistToAll(QString playerlist);


    // These signals are for Message Manager
    void signalSendSnapshot(QString snapshot);
    void signalFire(int tank_id);
    void signalStopFire(int tank_id);
    void signalMove(int tank_id);
    void signalStopMove(int tank_id);
    void signalTurn(int tank_id, char direction);
    void signalAcceptNickname(int localDescriptor, int player_id);
    void signalRejectNickname(int localDescriptor, QString message);
    void signalDisconnect();
    void signalSendMap(int width, int height, const QString& map, int health, int motion_speed, int bullet_speed, int fire_rate);

protected:

    QString constructPlayerlist();
    QString constructSnapshot();

    QVector<int> m_startPositions;
    QString my_nick;
    QString m_map;

    ServerMessageManager * m_ptr_messageManager;

    CRITICAL_SECTION m_cs_snapshot;
};
